home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / pyshared / PIL / GribStubImagePlugin.py < prev    next >
Text File  |  2006-12-03  |  2KB  |  69 lines

  1. #
  2. # The Python Imaging Library
  3. # $Id: GribStubImagePlugin.py 2134 2004-10-06 08:55:20Z fredrik $
  4. #
  5. # GRIB stub adapter
  6. #
  7. # Copyright (c) 1996-2003 by Fredrik Lundh
  8. #
  9. # See the README file for information on usage and redistribution.
  10. #
  11.  
  12. import Image, ImageFile
  13.  
  14. _handler = None
  15.  
  16. ##
  17. # Install application-specific GRIB image handler.
  18. #
  19. # @param handler Handler object.
  20.  
  21. def register_handler(handler):
  22.     global _handler
  23.     _handler = handler
  24.  
  25. # --------------------------------------------------------------------
  26. # Image adapter
  27.  
  28. def _accept(prefix):
  29.     return prefix[0:4] == "GRIB" and prefix[7] == chr(1)
  30.  
  31. class GribStubImageFile(ImageFile.StubImageFile):
  32.  
  33.     format = "GRIB"
  34.     format_description = "GRIB"
  35.  
  36.     def _open(self):
  37.  
  38.         offset = self.fp.tell()
  39.  
  40.         if not _accept(self.fp.read(8)):
  41.             raise SyntaxError("Not a GRIB file")
  42.  
  43.         self.fp.seek(offset)
  44.  
  45.         # make something up
  46.         self.mode = "F"
  47.         self.size = 1, 1
  48.  
  49.         loader = self._load()
  50.         if loader:
  51.             loader.open(self)
  52.  
  53.     def _load(self):
  54.         return _handler
  55.  
  56. def _save(im, fp, filename):
  57.     if _handler is None or not hasattr("_handler", "save"):
  58.         raise IOError("GRIB save handler not installed")
  59.     _handler.save(im, fp, filename)
  60.  
  61.  
  62. # --------------------------------------------------------------------
  63. # Registry
  64.  
  65. Image.register_open(GribStubImageFile.format, GribStubImageFile, _accept)
  66. Image.register_save(GribStubImageFile.format, _save)
  67.  
  68. Image.register_extension(GribStubImageFile.format, ".grib")
  69.